package action;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import entity.Dir;
import entity.Note;
import entity.User;

import service.NoteService;

public class NoteAction extends BaseAction {
	@Resource(name = "noteService")
	private NoteService noteService;
	private int noteId;
	private Note note;
	private List<Note> notes;
	private int rowCount=0;
	private int pageNo=1;
	private int pageSize = 5;
	private int dirIndex;
	private int dirId;
	private String dirName;

	public int getDirId() {
		return dirId;
	}

	public void setDirId(int dirId) {
		this.dirId = dirId;
	}
	public String getDirName() {
		return dirName;
	}

	public void setDirName(String dirName) {
		this.dirName = dirName;
	}

	public int getNoteId() {
		return noteId;
	}

	public void setNoteId(int noteId) {
		this.noteId = noteId;
	}
	public Note getNote() {
		return note;
	}

	public void setNote(Note note) {
		this.note = note;
	}
	public int getDirIndex() {
		return dirIndex;
	}

	public void setDirIndex(int dirIndex) {
		this.dirIndex = dirIndex;
	}

	public int getRowCount() {
		return rowCount;
	}

	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	public List<Note> getNotes() {
		return notes;
	}

	public void setNotes(List<Note> notes) {
		this.notes = notes;
	}
	public NoteService getNoteService() {
		return noteService;
	}
	public void setNoteService(NoteService noteService) {
		this.noteService = noteService;
	}
	
	public String note() {
		User u = (User)session.get("user");
		List<Dir> dirs =  noteService.listDirsByUser(u.getId());
		notes = noteService.listNoteByUser(u.getId(), pageNo, pageSize);
		rowCount = noteService.countNumByUser(u.getId());
		session.put("dirs",dirs);	
		session.put("rowCount", rowCount);  //session存储日志总数
		session.put("currentDir", null);    //session存储单前目录  null为根目录
		return "note";
	}
	
	public String editNote() {
		return "editnote";
	}	
	
	public String updateNote() {
		User u = (User)session.get("user");
		note = noteService.findNoteById(noteId, u.getId());
		return "updatenote";
	}
	
	public String selectDir() {
		List<Dir> dirs = (List<Dir>) session.get("dirs");
		Dir currentDir = dirs.get(dirIndex);
		rowCount = noteService.countNumByDir(currentDir.getId());
		session.remove("rowCount");
		session.remove("currentDir");
		session.put("rowCount", rowCount);     //session更新日志总数
		session.put("currentDir", currentDir);   //session更新单前目录  null为根目录
		return "success";
	}
	
	public String selectPage() {		
		User u = (User)session.get("user");
		Dir currentDir = (Dir)session.get("currentDir");
		if(currentDir == null) {
			notes = noteService.listNoteByUser(u.getId(), pageNo, pageSize);
		}else {
			notes = noteService.listNoteByDir(currentDir.getId(), pageNo, pageSize);
		}
		return "note";
	}
	
	public String noteCheck() {
		User u = (User)session.get("user");
		note = noteService.findNoteById(noteId, u.getId());
		return "notecheck";
	}
	
	public String saveNote() {
		List<Dir> dirs = (List<Dir>) session.get("dirs");
		Dir currentDir = dirs.get(dirIndex);
		noteService.dirPlus(currentDir.getId());          //目录nCount +1
		note.setDir(currentDir);
		note.setCreateTime(new Date());
		note.setUser((User)session.get("user"));
		note.setId(noteService.saveNote(note));
		return "notecheck";
	}
	
	public String deleteNote() {
		User u = (User)session.get("user");
		Dir d = noteService.findDirByNote(noteId);
		noteService.dirMinus(d.getId());                  //目录nCount -1
		noteService.deleteNote(noteId, u.getId());
		return "success";
	}
	
	public String saveUpNote() {
		note.setCreateTime(new Date());
		note.setUser((User)session.get("user"));
		note.setDir(noteService.findDirByNote(note.getId()));
		noteService.updateNote(note);
		return "notecheck";
	}
	
	public String editDir() {
		return "editdir";
	}
	//修改
	public String rnDir() throws IOException {
	    noteService.rnDir(dirId,dirName);
		response.getWriter().println("{\"info\":\"重命名成功！\",\"status\":\"y\"}");
		return null;
	}
	
	public String delDir() throws IOException {
		noteService.delDir(dirId);
		response.getWriter().println("{\"info\":\"删除成功！\",\"status\":\"y\"}");
		return null;
	}
	
	public String addDir() throws IOException {
		Dir dir = new Dir();
		dir.setDirName(dirName);
		dir.setCreateTime(new Date());
		dir.setnCount(0);
		dir.setUser((User)session.get("user"));
		int id = noteService.svDir(dir);
		response.getWriter().println("{\"info\":\"添加目录成功！\",\"categoryId\":\""+id+"\"}");
		return null;
	}
}
